package com.bysj.service;

import com.bysj.dao.TopicDao;
import com.bysj.entity.Topic;
import com.bysj.entity.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TopicServiceImpl implements TopicService {
    @Autowired
    TopicDao topicDao;

    //添加贴子
    @Override
    public void addTopic(Topic topic) {
        SimpleDateFormat fmt= new SimpleDateFormat("yyyy-MM-dd");
        Date currentTime = new Date();
        String dateString = fmt.format(currentTime);
        topic.setCreatedate(dateString);

        topicDao.addTopic(topic);
    }
    //查询所有贴子
    @Override
    public List<Topic> getTopicAll(Map map) {
        return topicDao.getTopicAll(map);
    }
    //根据id查询贴子
    @Override
    public List<Topic> getTopicById(Integer id) {
        return topicDao.getTopicById(id);
    }

    @Override
    public List<Topic> getTopicByUid(Integer id) {
        return topicDao.getTopicByUid(id);
    }
    //根据贴子id删除贴子
    @Override
    public void deleteTopicById(Integer id) {
         topicDao.deleteTopicById(id);

    }
    //根据输入的文字查询相关贴子
    @Override
    public List<Topic> getTopicByWords(String searchwords) {
        return topicDao.getTopicByWords(searchwords);
    }

    @Override
    public List<Topic> getTopicByCategoryName(String categoryname) {
        return topicDao.getTopicByCategoryName(categoryname);
    }
    //分页查询所有贴子
    @Override
    public PageInfo<Topic> getAllTopicByPage(Map map, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Topic> topicList = topicDao.getTopicAll(map);
        PageInfo<Topic> pageInfo = new PageInfo<Topic>(topicList);
        return pageInfo;
    }
}
